const path = require('path')
const { defineConfig } = require('rollup')
const json = require('@rollup/plugin-json')
const alias = require('@rollup/plugin-alias')
const terser = require('@rollup/plugin-terser')
const commonjs = require('@rollup/plugin-commonjs')
const typescript = require('@rollup/plugin-typescript')
const nodeResolve = require('@rollup/plugin-node-resolve')
const dts = require('rollup-plugin-dts').default

// const entryFile = path.resolve(__dirname, '../../src/cli/index.ts');
// const outputDir = path.resolve(__dirname, '../../lib');

// 路径别名配置
const aliasConfig = {
  entries: [{ find: '@', replacement: path.resolve(__dirname, '../../src') }]
}

const tsconfig = require('./tsconfig.json')
tsconfig.compilerOptions.module = 'ESNext'

module.exports = defineConfig([
  {
    input: './src/bin/main.ts', // 入口文件路径
    output: {
      dir: './dist',
      format: 'cjs', // 输出格式为CommonJS模块
      exports: 'named',
      preserveModules: true, // 保留模块结构
      // preserveModulesRoot: 'src', // 将保留的模块放在根级别的此路径下
      sourcemap: false // 是否生成源映射文件
    },
    plugins: [
      typescript(tsconfig),
      nodeResolve(),
      commonjs(),
      json(),
      terser({
        format: {
          comments: false, // 禁用注释
          beautify: true // 保留代码格式
        },
        mangle: true // 代码混淆
      }),
      // 路径别名
      alias(aliasConfig)
    ],
    external: []
    // watch: isDev, // 是否开启监控实时更新
  },
  // 打包暴露文件
  {
    input: './src/index.ts',
    output: {
      file: './dist/index.js',
      format: 'cjs'
    },
    plugins: [typescript(tsconfig), alias(aliasConfig)]
  },
  // 打包类型声明文件
  {
    input: './src/index.ts',
    output: {
      file: './dist/index.d.ts'
    },
    plugins: [dts()]
  }
])
